package com.fypg.module.medical.service.outpatientorder;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.fypg.module.medical.controller.admin.outpatientorder.vo.*;
import com.fypg.module.medical.dal.dataobject.outpatientorder.OutpatientOrderDO;
import com.fypg.framework.common.pojo.PageResult;
import com.fypg.framework.common.pojo.PageParam;
import com.fypg.framework.common.util.object.BeanUtils;

import com.fypg.module.medical.dal.mysql.outpatientorder.OutpatientOrderMapper;

import static com.fypg.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.fypg.framework.common.util.collection.CollectionUtils.convertList;
import static com.fypg.framework.common.util.collection.CollectionUtils.diffList;
import static com.fypg.module.medical.enums.ErrorCodeConstants.*;

/**
 * 门诊订单信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OutpatientOrderServiceImpl implements OutpatientOrderService {

    @Resource
    private OutpatientOrderMapper outpatientOrderMapper;

    @Override
    public String createOutpatientOrder(OutpatientOrderSaveReqVO createReqVO) {
        // 插入
        OutpatientOrderDO outpatientOrder = BeanUtils.toBean(createReqVO, OutpatientOrderDO.class);
        outpatientOrderMapper.insert(outpatientOrder);
        // 返回
        return outpatientOrder.getId();
    }

    @Override
    public void updateOutpatientOrder(OutpatientOrderSaveReqVO updateReqVO) {
        // 校验存在
        validateOutpatientOrderExists(updateReqVO.getId());
        // 更新
        OutpatientOrderDO updateObj = BeanUtils.toBean(updateReqVO, OutpatientOrderDO.class);
        outpatientOrderMapper.updateById(updateObj);
    }

    @Override
    public void deleteOutpatientOrder(String id) {
        // 校验存在
        validateOutpatientOrderExists(id);
        // 删除
        outpatientOrderMapper.deleteById(id);
    }

    @Override
        public void deleteOutpatientOrderListByIds(List<String> ids) {
        // 校验存在
        validateOutpatientOrderExists(ids);
        // 删除
        outpatientOrderMapper.deleteByIds(ids);
        }

    private void validateOutpatientOrderExists(List<String> ids) {
        List<OutpatientOrderDO> list = outpatientOrderMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(OUTPATIENT_ORDER_NOT_EXISTS);
        }
    }

    private void validateOutpatientOrderExists(String id) {
        if (outpatientOrderMapper.selectById(id) == null) {
            throw exception(OUTPATIENT_ORDER_NOT_EXISTS);
        }
    }

    @Override
    public OutpatientOrderDO getOutpatientOrder(String id) {
        return outpatientOrderMapper.selectById(id);
    }

    @Override
    public PageResult<OutpatientOrderDO> getOutpatientOrderPage(OutpatientOrderPageReqVO pageReqVO) {
        return outpatientOrderMapper.selectPage(pageReqVO);
    }

}